/*
 * Created by JFormDesigner on Sun Nov 28 21:51:48 CST 2021
 */

package com.xxbb.chapter_10.login_frame;


import com.xxbb.chapter_10.dao.BaseDao;
import com.xxbb.chapter_10.main_frame.MainFrame;
import com.xxbb.chapter_10.struct.SqlStruct;

import java.awt.*;
import java.awt.event.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.*;

/**
 * @author unknown
 */
public class LoginFrame extends JFrame {
    public LoginFrame() {
        initComponents();
    }


    private void buttonCloseMouseClicked(MouseEvent e) {
        // TODO add your code here
        System.exit(0);
    }

    private void button2MouseClicked(MouseEvent e) {
        // TODO add your code here
    }

    private void buttonLoginMouseClicked(MouseEvent e) {
        // TODO add your code here
        String account = textFieldAccount.getText();
        String password = (String) passwordFieldPassword.getText();

        ResultSet res = BaseDao.ExecuteQuery(SqlStruct.login, account);
        try {
            assert res != null;
            if (res.next()) {
                if (res.getString("password").equals(password)) {
                    JOptionPane.showMessageDialog(null, "登录成功");
                    MainFrame mainFrame = new MainFrame();
                    mainFrame.setVisible(true);
                    this.dispose();
                } else {
                    JOptionPane.showMessageDialog(null, "用户名或者密码错误！");
                }
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }

    }


    private void initComponents() {
        //自定义属性
        setUndecorated(true);//设置无边框
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - unknown
        label1 = new JLabel();
        label2 = new JLabel();
        textFieldAccount = new JTextField();
        passwordFieldPassword = new JPasswordField();
        label3 = new JLabel();
        buttonLogin = new JButton();
        buttonClose = new JButton();

        //======== this ========
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u8d26\u53f7\uff1a");
        contentPane.add(label1);
        label1.setBounds(new Rectangle(new Point(65, 85), label1.getPreferredSize()));

        //---- label2 ----
        label2.setText("\u5bc6\u7801\uff1a");
        contentPane.add(label2);
        label2.setBounds(65, 130, 50, 17);
        contentPane.add(textFieldAccount);
        textFieldAccount.setBounds(130, 75, 195, textFieldAccount.getPreferredSize().height);
        contentPane.add(passwordFieldPassword);
        passwordFieldPassword.setBounds(130, 122, 195, passwordFieldPassword.getPreferredSize().height);

        //---- label3 ----
        label3.setText("\u767b\u5f55\u4e2d\u5fc3");
        label3.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 26));
        contentPane.add(label3);
        label3.setBounds(new Rectangle(new Point(150, 15), label3.getPreferredSize()));

        //---- buttonLogin ----
        buttonLogin.setText("\u7acb\u5373\u767b\u5f55");
        buttonLogin.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                buttonLoginMouseClicked(e);
            }
        });
        contentPane.add(buttonLogin);
        buttonLogin.setBounds(new Rectangle(new Point(130, 170), buttonLogin.getPreferredSize()));

        //---- buttonClose ----
        buttonClose.setText("\u5173\u95ed");
        buttonClose.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button2MouseClicked(e);
                buttonCloseMouseClicked(e);
            }
        });
        contentPane.add(buttonClose);
        buttonClose.setBounds(new Rectangle(new Point(255, 170), buttonClose.getPreferredSize()));

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(400, 300);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - unknown
    private JLabel label1;
    private JLabel label2;
    private JTextField textFieldAccount;
    private JPasswordField passwordFieldPassword;
    private JLabel label3;
    private JButton buttonLogin;
    private JButton buttonClose;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
